package m202405.d12;

/**
 * @@author: 爱做梦的锤子
 * @date: 2024/5/12 12:44
 */
public class Q238 {
    public int[] productExceptSelf1(int[] nums) {
        int[] l = new int[nums.length];
        int[] r = new int[nums.length];
        l[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            l[i] = l[i - 1] * nums[i - 1];
        }
        r[nums.length - 1] = 1;
        for (int i = nums.length - 2; i >= 0; i--) {
            r[i] = r[i + 1] * nums[i + 1];
        }
        int[] rs = new int[nums.length];
        for (int i = 0; i < nums.length; i++) {
            rs[i] = l[i] * r[i];
        }
        return rs;
    }

    public int[] productExceptSelf(int[] nums) {
        int[] rs = new int[nums.length];
        rs[0] = 1;
        for (int i = 1; i < nums.length; i++) {
            rs[i] = rs[i - 1] * nums[i - 1];
        }
        int last = nums[nums.length - 1];
        for (int i = nums.length - 2; i >= 0; i--) {
            rs[i] = rs[i] * last;
            last = last * nums[i];
        }
        return rs;
    }
}
